﻿package newx.com.control.list
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import newx.com.NxUIComponent;
	import newx.com.ResManager;
	import newx.com.control.IList;
	import newx.com.control.NxClip;
	import newx.com.control.Scroll.NxScrollBar;
	import newx.com.control.Scroll.NxScrollEvent;
	import newx.com.control.Scroll.NxScrollSkin;
	
	/**
	 * @
	 * 
	 */	
	public class NxList extends NxUIComponent implements IList
	{
//		/**默认宽度*/
//		private var defaultWidth		:uint	=	100;
		
//		/**默认高度*/
//		private var defaultHeight		:uint	=	100;
		
		private const MinSliderSize	:Number=20; 
		
		private var _defaultTxtColor	:uint	=	0xFFCC00;
		
		//==================================================================
		
		/**列表数据[{label:"",data:null},{label:"",data:null}...]*/
		private var _dataProvider		:Array=[];
		
		/**最大显示行数*/
		private var _showCount			:uint	=	4;
		
		/**单元格对齐方式*/
		private var _itemAlign			:String	=	TextFieldAutoSize.LEFT;
		
		/**皮肤数组，格式[列表项皮肤，纵向滚动条皮肤，横向滚动条皮肤]*/
		private var _skinStrArr		:Array=["TabelBGBtn_1","vScrol_1","vScrol_2"];
		
		/**皮肤字符串，格式“列表项皮肤，纵向滚动条皮肤，横向滚动条皮肤”*/
		private var _skinStr			:String="TabelBGBtn_1";
		
		private var _vScrollSkin 		:String="vScrol_1";
		
		private var _hScrollSkin		:String="vScrol_2";
		
		/**滚动组件*/
//		private var _scrollPanel		:NxScrollPanel;
		
		/**存放item显示对象的容器*/
		private var _itemSpace			:Sprite;
		
		/**列表背景*/
		private var _listBgBitm		:Bitmap;
		
		/**单元格高度*/
		private var _itemHeight		:int=25;
		
		/***/
		private var _itemWidth			:int=120;
		
//		/**鼠标经过的特效*/
//		private var _rollOverSkin		:MovieClip;
		
		/**新鼠标滑过状态*/
		private var _overStateDis		:Sprite;
		
		private var _downStateDis		:Sprite;
//		/**鼠标按下的特效*/
//		private var _mouseDownSkin		:MovieClip;
		
		/**当前被鼠标悬停对象索引*/
		private var _currentRollIndex	:int	=	-1;
		
		/**当前被选中项*/
		private var _currentSelect		:int	=	-1;
		
		/**图标x偏移量*/
		private var _iconOffSizeX		:int	=	0;
		
		/**图标y偏移量*/
		private var _iconOffSizeY		:int	=	0;
		
		/**抽象（虚拟）列表*/
		private var _abstractList:Object={x:0,y:0,width:300,height:200}
			
			
		/**
		 * 当前显示的位置占可滑动总长度的百分比。【0,1】，默认为0
		 * */
		private var _curPerDis			:Number=0;
		//显示层
		private var  _listBg:Sprite
		
		private var _interActiveSpace:Sprite;
		
		private var _dataSpace:Sprite;
		
		private var _iconSpace:Sprite;
		
		private var _scrollBarSpace:Sprite;
		
		private var _txtContainerArr:Array=new Array();
		
		private var _iconContainerArr:Array=new Array();
		
		private var _isDrawnAtOnce:Boolean=false;
		
		private var _tableChanged:Boolean=true;
		//显示层END
		
		/**索引偏移*/
		private var _indexOffset:int=0;
		
		private var _lastIndexOffset:int=0;
		/**背景y方向偏移*/
		private var _yOffset:Number=0;
		
		private var _txtFormat:TextFormat;
		
		private var _verticalBar:NxScrollBar;
		
		private var _horizontalBar:NxScrollBar;
		
//		private var _lastIndexOffset
		
		private var _isLocked		:Boolean=false;
		
		/**上次滑动条的位置*/
		private var _customPerDis	:Number=0;
		
		public function NxList()
		{
			super();
			Init();			
		}
		
		public function get iconOffSizeY():int
		{
			return _iconOffSizeY;
		}

		public function set iconOffSizeY(value:int):void
		{
			_iconOffSizeY = value;
			
			if(_isDrawnAtOnce)
			{
				updateDisplay();
			}
			else
			{
				_tableChanged=true;
			}
		}

		public function get iconOffSizeX():int
		{
			return _iconOffSizeX;
		}

		public function set iconOffSizeX(value:int):void
		{
			if(_iconOffSizeX != value)
			{
				_iconOffSizeX = value;
				if(_isDrawnAtOnce)
				{
					updateDisplay();
				}
				else
				{
					_tableChanged=true;
				}
			}			
		}

		override public function set width(value:Number):void
		{
			_itemWidth	=	value;
			_abstractList.width=_itemWidth;
			if(_isDrawnAtOnce)
			{
				updateDisplay();
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		override public function get width():Number
		{
			return _abstractList.width;
		}
		
		override public function set height(value:Number):void
		{
			return;
//			_abstractList.height=value;
		}
		
		override public function get height():Number
		{
//			return _abstractList.height;
			return _showCount*_itemHeight;
		}
		
		public function get dataProvider():Array
		{
			return _dataProvider;
		}
		
		public function set dataProvider(value:Array):void
		{
			//防止value\_dataProvider为空时，取length报错。
			if(value==null)
			{
				_dataProvider.length=0;
				_abstractList.height=_dataProvider.length*_itemHeight;
			}
			else
			{
				_dataProvider	=	value;
				_abstractList.height=_dataProvider.length*_itemHeight;
			}
			
			if(_isDrawnAtOnce)
			{
//				scrollDisplay();
//				_indexOffset=int(total*_curPerDis);//更新偏移量
				updateNxScrollPanel();
				updateContainer();
				updateData();
				drawIcon();
				
				_currentSelect=-1;
				_downStateDis.visible=false;
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		public function get currentSelect():int
		{
			return _currentSelect;
		}
		
		public function set currentSelect(value:int):void
		{
			if(value != _currentSelect)
			{
				_currentSelect			=	value;
				
				if(_isDrawnAtOnce)
				{
					if(value < 0)
					{
						_downStateDis.visible	=	false;
						//					_listBg.removeChild(
					}
					else
					{
						_downStateDis.visible	=	true;
						_downStateDis.y	=	_itemHeight*(_currentSelect-_indexOffset);
					}
				}
				else
				{
					_tableChanged=true;
				}
				
				var event:NxListEvent=new NxListEvent(
					NxListEvent.LIST_CLICK,dataProvider[_currentSelect]);
				
				event.id=_currentSelect;
				
				dispatchEvent(event);
			}			
		}
		
		public function getItemByIndex(value:int):Object
		{
			return _dataProvider[value];
		}
		
		public function getIndex(func:Function,pList:Array):int
		{
			var tSize:int	=	_dataProvider.length;
			
			for(var i:int = 0 ; i < tSize ; i ++)
			{
				if(func.call(this,_dataProvider[i],pList))
				{
					return i;
				}
			}
			return -1;
		}
		
		public function get showCount():uint
		{
			return _showCount;
		}
		
		public function set showCount(value:uint):void
		{
			if(value == _showCount)	return;
			
			_showCount	=	value;
			_abstractList.height=_showCount*_itemHeight;
			
			if(_isDrawnAtOnce)
			{
				updateDisplay();
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		public function get itemSkin():String
		{
			return _skinStr;
		}
		
		public function set itemSkin(value:String):void
		{
			if(_skinStr == value) return;
			
			_skinStr	=	value;
				
			_skinStrArr[0] = _skinStr;
			
			if(_isDrawnAtOnce)
			{
				updateDisplay();
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		public function get vScrollSkin():String
		{
			return _vScrollSkin;
		}
		
		public function set vScrollSkin(val:String):void
		{
			_vScrollSkin = val;
			
			_skinStrArr[1] = val;
			
			if(_isDrawnAtOnce)
			{
				updateDisplay();
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		public function get hScrollSkin():String
		{
			return _hScrollSkin;
		}
		
		public function set hScrollSkin(val:String):void
		{
			_hScrollSkin = val;
			
			_skinStrArr[2] = val;
			
			if(_isDrawnAtOnce)
			{
				updateDisplay();
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		public function addItem(value:Object,index:int = -1):void
		{
			if(index < 0)
			{
				_dataProvider.push(value);
				_abstractList.height=_dataProvider.length*_itemHeight;
			}
			else
			{
				_dataProvider.splice(index,0,value);
				_abstractList.height=_dataProvider.length*_itemHeight;
			}
			
			if(_isDrawnAtOnce)
			{
				updateNxScrollPanel();
				updateContainer();
				updateData();
				drawIcon();
				
				_currentSelect=-1;
				_downStateDis.visible=false;
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		public function setItem(value:Object,index:int):void
		{
			_dataProvider[index]	=	value;
//			setData();
			if(_isDrawnAtOnce)
			{
				updateData();
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		
		/***/
		public function removeItem(value:uint):void
		{
			if(value>_dataProvider.length-1)	return;
			
			_dataProvider.splice(value,1);
			_abstractList.height=_dataProvider.length*_itemHeight;

			
			if(_isDrawnAtOnce)
			{
				updateNxScrollPanel();
				updateContainer();
				updateData();
				drawIcon();
				
				_currentSelect=-1;
				_downStateDis.visible=false;
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		public function clearList():void
		{
			if(_dataProvider==null)
			{
				return;
			}
			
			_dataProvider=[];
			_abstractList.height=_dataProvider.length*_itemHeight;
			
			if(_isDrawnAtOnce)
			{
				updateNxScrollPanel();
				updateContainer();
				updateData();
				drawIcon();
				
				_currentSelect=-1;
				_downStateDis.visible=false;
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		public function get itemAlign():String
		{
			return _itemAlign;
		}
		
		public function set itemAlign(value:String):void
		{
			if(_itemAlign == value)return;
			
			_itemAlign = value;
				
//				updateItemSize();
			if(_isDrawnAtOnce)
			{
				updateDisplay();
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		/**
		 * 全部重画一遍，
		 * 
		 * 注意：
		 * 顺序要求，无论何时先绘制滚动条，滚动条是否在舞台，及在舞台时滑块的位置，
		 * 将影响数据层的_indexOffset,_yOffset值，它们将影响如何放置容器，及如何绘制数据。
		 */
		private function updateDisplay():void
		{	
			drawScrollBar();
			
			updateContainer();
			
			drawBg();	
			
			drawData();
			
			drawIcon();
			
			drawInterActiveState();
			
			this.scrollRect=new Rectangle(0,0,_itemWidth,_itemHeight*_showCount);
			
			_tableChanged=false;
		}
		
		public function set isSliderLocked(value:Boolean):void
		{
			_isLocked=value;
		}
		public function get isSliderLocked():Boolean
		{
			return _isLocked;
		}
		
		public function set sliderPerDis(value:Number):void
		{
			_customPerDis=value;
			
			if(_isDrawnAtOnce)
			{
				updateNxScrollPanel();
				updateContainer();
				updateData();
				drawIcon();
				
				_currentSelect=-1;
				_downStateDis.visible=false;
			}
			else
			{
				_tableChanged=true;
			}
		}
		
		public function get sliderPerDis():Number
		{
			return _curPerDis;
		}
		/**
		 * 初始化
		 */		
		private function Init():void
		{
//			_itemSpace		=	new Sprite();
//			_listBgBitm		=	new Bitmap();
			
//			_itemSpace.addChild(_listBgBitm);
			
//			_itemSpace.addEventListener(MouseEvent.CLICK		,onClick);
//			_itemSpace.addEventListener(MouseEvent.MOUSE_MOVE	,onMouseMove);
//			_itemSpace.addEventListener(MouseEvent.ROLL_OUT		,onMouseMoveOut);
			
			
			_txtFormat=new TextFormat();
			_txtFormat.align=TextFormatAlign.CENTER;
			_txtFormat.color=_defaultTxtColor;
			
			_listBg		=new Sprite();
			
			_interActiveSpace=new Sprite();
			_interActiveSpace.mouseChildren=false;
			_interActiveSpace.mouseEnabled=false;
			
			_dataSpace	=new Sprite();
			_dataSpace.mouseChildren=false;
			_dataSpace.mouseEnabled=false;
			
			_iconSpace	=new Sprite();
			_iconSpace.mouseChildren=false;
			_iconSpace.mouseEnabled=false;
			
			_scrollBarSpace=new Sprite();
			
			drawData();//准备数据容器
			
			addEventListener(Event.ADDED_TO_STAGE,onStageHandler);
			
			addChildAt(_listBg,0);
			addChildAt(_interActiveSpace,1);
			addChildAt(_dataSpace,2);
			addChildAt(_iconSpace,3);
			addChildAt(_scrollBarSpace,4);
			

		}
		
		/**滚动显示区域*/
		private function scrollDisplay(e:NxScrollEvent=null):void
		{			
			updateOffset();
				
			if(_indexOffset!=_lastIndexOffset)
			{
				updateFlashOptimise();
				
				updateData();
				
				drawIcon();
				
				_lastIndexOffset=_indexOffset;
				
				_downStateDis.visible=false;
				
				_currentSelect=-1;
			}
			
			updateContainer();
		}
		
		
		
//		/**
//		 * 刷新/创建显示对象
//		 * */
//		private function updateDisplay():void
//		{
//
//			//绘制背景
////			drawListBG();	
////			updateBg();
//			
//			//设置文本
////			setData()
//		}
		
		
//		/**
//		 * 绘制列表背景
//		 * */
//		private function drawListBG():void
//		{
//			var time1:int=getTimer();
//			
//			if(_skinStrArr.length != 3)
//			{
//				return;
//			}
//			
//			//保存scroll位置
//			var scrollDis	:Number = 0;
//			if(_scrollPanel != null){
//				scrollDis = 	_scrollPanel.getScrollValY(_scrollPanel.cTcY);
//			}
//			
//			//清除旧背景			
//			if(_scrollPanel != null)
//			{
//				removeChild(_scrollPanel);
//				_scrollPanel	=	null;
//			}
//			
//			if(_rollOverSkin !=null && _rollOverSkin.parent == _itemSpace)
//			{
//				_itemSpace.removeChild(_rollOverSkin);
//				_itemSpace.removeChild(_mouseDownSkin);
//			}
//			
//			_rollOverSkin	=	ResManager.newResClass(
//				_skinStrArr[0])["Button_over"] as MovieClip;
//			
//			_mouseDownSkin	=	ResManager.newResClass(
//				_skinStrArr[0])["Button_down"] as MovieClip;
//			
//			_rollOverSkin.visible	=	false;
//			_mouseDownSkin.visible	=	false;
//			
//			_itemSpace.addChildAt(_rollOverSkin,1);
//			_rollOverSkin.y=0;
//			_itemSpace.addChildAt(_mouseDownSkin,1);
//			_mouseDownSkin.y=0;
//			
//			_mouseDownSkin.mouseChildren	=	false;
//			_rollOverSkin.mouseChildren		=	false;
//			_mouseDownSkin.mouseEnabled		=	false;
//			_rollOverSkin.mouseEnabled		=	false;
//			
//			_rollOverSkin.width	=	_mouseDownSkin.width	=	defaultWidth;
//			
//			//创建临时容器
//			var tClip		:Sprite	=	new Sprite();
//			
//			if(dataProvider == null)
//			{
//				return;
//			}
//			
//			//临时行数
//			var tRow		:int	=	dataProvider.length;
//			
//			if(tRow==0||showCount==0)
//			{ 
//				return;
//			}
//			
//			if(tRow <showCount)
//			{
//				tRow=showCount;
//			}
//			
//			//单元格
//			var tItem		:MovieClip;
//						
//			tItem		=	ResManager.newResClass(
//				_skinStrArr[0])["Button_normal"] as MovieClip;
//			
//			//记录单元格高度
//			_itemHeight	=	tItem.height;
//			
//			//记录显示区域高度
//			defaultHeight	=	_itemHeight*showCount;
//			
//			//绘制表身
//			for(var i:int ; i < tRow ; i ++)
//			{
//				tItem				=	ResManager.newResClass(
//					_skinStrArr[0])["Button_normal"] as MovieClip;
//				tItem.width			=	defaultWidth;
//				tItem.x				=	0;
//				tItem.y				=	_itemHeight*i;
//				
//				tClip.addChild(tItem);
//			}
//			
//			//创建表格位图
//			var tmpBitMapData:BitmapData	=	new BitmapData(
//				defaultWidth,_itemHeight*tRow,true,0x00000000);
//			tmpBitMapData.draw(tClip);
//			
//			tClip	=	null;
//			
//			if(_listBgBitm.bitmapData != null)
//			{
//				_listBgBitm.bitmapData.dispose();
//			}
//			
//			_listBgBitm.bitmapData	=	tmpBitMapData;
//			
//			
//			
//			_scrollPanel	=	new NxScrollPanel(
//				new Rectangle(0,0,defaultWidth+1,defaultHeight),
//				_itemSpace,
//				new NxScrollSkin(_skinStrArr[1],_skinStrArr[2])
//			);	
//
//			addChild(_scrollPanel);
//			
//			//计算位置,欢迎滚动条位置
//			_scrollPanel.updateScroll(scrollDis);
////			_scrollPanel.update();
//			
//			var time2:int=getTimer();
//			trace("NXSCROLL－－－－－－－"+(time2-time1));
//		}
		

		/**显示区更新前,显示优化*/
		
		private function updateFlashOptimise():void
		{
			while(_iconSpace.numChildren>0)
			{
				_iconSpace.removeChildAt(0);
			}
		}
		
		private function updateOverState():void
		{
			
		}
		
		private function updateNxScrollPanel():void
		{
			if(_verticalBar==null)
			{
				if(_abstractList.height>_showCount*_itemHeight)
				{
					drawScrollBar();
				}
			}
			else	
			{
				if(_abstractList.height>_showCount*_itemHeight)
				{
					if(_verticalBar.parent==null)	_scrollBarSpace.addChild(_verticalBar);
					_verticalBar.x=_itemWidth-_verticalBar.width;
					
					_verticalBar.sliderMinLen=MinSliderSize;
					_verticalBar.scrollDistance=_verticalBar.slideableScope/(_dataProvider.length-_showCount);
					_verticalBar.addEventListener(NxScrollEvent.UPDATE_AREA,scrollDisplay);
					
					if(!_isLocked)
					{
						_verticalBar.setSlider(_customPerDis);
					}
				}
				else
				{
					if(_verticalBar.parent)	_scrollBarSpace.removeChild(_verticalBar);
					_verticalBar.removeEventListener(NxScrollEvent.UPDATE_AREA,scrollDisplay);
				}
			}
			
			updateOffset();
		}
		
		/**更新偏移量，每次使用偏移量之前计算出当前偏移量
		 * 只有这一个以update或draw为前缀的是对数据的操作，其余都是显示层的
		 * 
		 * */
		//每次偏移量改变时修改偏移量，可以节约计算，但容易遗漏情况。
		private function updateOffset():void
		{
			if(_verticalBar&&_verticalBar.parent)
			{
				_curPerDis=_verticalBar.sliderPerY;
				
				var total:int=_dataProvider.length>_showCount?_dataProvider.length-_showCount :0;
				
				_indexOffset=int(total*_curPerDis);//偏移量
				
				_yOffset=-_itemHeight*(total*_curPerDis%1);
			}
			else
			{
				_curPerDis=0;
				_indexOffset=0;
				_yOffset=0;
			}
		}
		
		/**
		 * 根据当前显示位置占总位置的百分比显示相应的文字。
		 * 
		 * */
		private function updateData():void
		{
//			var total:int=_dataProvider.length>_showCount?_dataProvider.length-_showCount :0;
//			
//			_indexOffset=total*_curPerDis>1?int(total*_curPerDis-1):0;//偏移量
			
			//始终多更新一个
			var rowNum:int=_showCount+1;
			
			for(var i:int=0;i<rowNum;i++)
			{
				if(null		==	_txtContainerArr[i])	continue;
				
				if(_dataProvider[_indexOffset+i]&&_dataProvider[_indexOffset+i].hasOwnProperty("label"))
				{
					var txt:TextField=TextField(_txtContainerArr[i]);
					_txtFormat.align=_itemAlign;
					txt.defaultTextFormat	=	_txtFormat;
					txt.htmlText=  _dataProvider[_indexOffset+i]["label"];
					var h:Number=txt.height;
					txt.autoSize=TextFieldAutoSize.NONE;
					txt.width=_itemWidth;
					txt.height=h;
					txt.x=0;
					txt.y=i*_itemHeight+(_itemHeight-txt.height)/2						
					//txt.htmlText=_dataProvider[_indexOffset+i]["label"];
				}
				else
				{
					TextField(_txtContainerArr[i]).text="";
				}
			}
		}
		
		private function drawIcon():void
		{
			
			var i:int=0;
			
			//始终多更新一个
			var rowNum:int=_showCount+1;
			
			//全部移除
//			var len:int=_iconContainerArr.length
			while(_iconSpace.numChildren>0)
			{
				_iconSpace.removeChildAt(0);
			}
			_iconContainerArr.length=rowNum;
			
			//遍历填充新的icon
			for( i=0;i<rowNum;i++)
			{
				if(_dataProvider[_indexOffset+i]&&_dataProvider[_indexOffset+i]["icon"])
				{
					if(_iconContainerArr[i])
					{
						if(NxClip(_iconContainerArr[i]).source!=_dataProvider[_indexOffset+i]["icon"])
						{
							NxClip(_iconContainerArr[i]).source=_dataProvider[_indexOffset+i]["icon"];
						}
						_iconSpace.addChild(NxClip(_iconContainerArr[i]));
						
						NxClip(_iconContainerArr[i]).x=_iconOffSizeX;
						NxClip(_iconContainerArr[i]).y=i*_itemHeight+_iconOffSizeY;
					}
					else
					{
						var icon:NxClip	=	new NxClip();
						icon.defaultImage = null;
						icon.isCache	=	true;
						icon.source=_dataProvider[_indexOffset+i]["icon"];
						_iconSpace.addChild(icon);
						icon.x=_iconOffSizeX;
						icon.y=i*_itemHeight+_iconOffSizeY;
						_iconContainerArr[i]=icon;
					}
				}
				else
				{
					if(_iconContainerArr[i])
					{
						_iconContainerArr[i]=null
					}
				}
			}
		}
		
		private function drawBg():void
		{
			while(_listBg.numChildren>0)
			{
				_listBg.removeChildAt(0);
			}
			
			var item:DisplayObject;
			
			var bg:Sprite=new Sprite();
			
			//始终多画一条
			var bgNum:int=_showCount+1;
			
			var yOffset:int=0;
			
			var tempMc:DisplayObject;
			
			for(var i:int=0;i<bgNum;i++)
			{	
				tempMc	=	ResManager.newResClass(_skinStrArr[0]);
				
				if(tempMc	!=	null	&&	tempMc.hasOwnProperty("Button_normal"))
				{
					item=tempMc["Button_normal"] as DisplayObject;
				}
				else
				{
					item=new Sprite();
				}
					
				
				item.height=_itemHeight;
				item.width=_itemWidth;
				item.y=yOffset;
				bg.addChild(item);
				
				yOffset+=_itemHeight;
			}
			var bitMap:Bitmap=new Bitmap();
			var w:Number=bg.width?bg.width:1;
			var h:Number=bg.height?bg.height:1;
			
			var bmd:BitmapData=new BitmapData(w,h,true,0x000000);
			bmd.draw(bg);
			
			bitMap.bitmapData=bmd;
			
			_listBg.addChildAt(bitMap,0);
		}

		
		private function drawData():void
		{	
			//始终多画一条
			var rowNum:int=_showCount+1;
			
			var i:int=0;
			
			//移除前一次多画的文本
			var len:int=_txtContainerArr.length;
			
			if(len>rowNum)
			{
				for(i=rowNum;i<len;i++)
				{
					_dataSpace.removeChild(_txtContainerArr[i]);
				}
			}
			_txtContainerArr.length=rowNum;
			
			var yOffset:Number=0;
			
			//遍历填充新的文本
			for(i=0;i<rowNum;i++)
			{
				if(!_txtContainerArr[i])
				{
					var txt:TextField=new TextField();
					txt.selectable=false;
					txt.multiline=false;
					txt.wordWrap=false;

					txt.autoSize=TextFieldAutoSize.LEFT;

					_txtContainerArr[i]=txt;

					_dataSpace.addChild(txt);
				}
				yOffset+=_itemHeight;
			}
			updateData();
		}
		
		private function drawInterActiveState():void
		{
			if(_overStateDis==null)
			{
				var tempMc:DisplayObject	=	ResManager.newResClass(_skinStrArr[0]);
				
				if(tempMc != null && tempMc.hasOwnProperty("Button_over"))
				{
					_overStateDis	=	tempMc["Button_over"] as Sprite;
				}
				else
				{
					_overStateDis	=	new  Sprite();
				}
				
				tempMc	=	ResManager.newResClass(_skinStrArr[0]);
				
				if(tempMc	!=	null &&		tempMc.hasOwnProperty("Button_down")	)
				{
					_downStateDis	=	tempMc["Button_down"] as Sprite;
				}
				else
				{
					_downStateDis	=	new Sprite();
				}
				
				
				_downStateDis.height=_overStateDis.height=_itemHeight;
				_downStateDis.width=_overStateDis.width=_itemWidth;	
				
				_interActiveSpace.addChild(_overStateDis);
				_interActiveSpace.addChild(_downStateDis);
				_overStateDis.visible=false;
				_downStateDis.visible=false;
				
				if(_currentSelect>-1)
				{
					_downStateDis.visible	=	true;
					_downStateDis.y			=	_itemHeight*(_currentSelect-_indexOffset);
				}
				else
				{
					_downStateDis.visible	=	false;
				}				
			}
			else
			{
				
				_overStateDis.visible=false;
				_downStateDis.visible=false;
				_downStateDis.height=_overStateDis.height=_itemHeight;
				_downStateDis.width=_overStateDis.width=_itemWidth;	
				
				if(_currentSelect>-1)
				{
					_downStateDis.visible	=	true;
					_downStateDis.y			=	_itemHeight*(_currentSelect-_indexOffset);
				}
				else
				{
					_downStateDis.visible	=	false;
				}	
			}
		}
		
		

		private function drawScrollBar():void
		{
			
			_abstractList.width=_itemWidth;
			_abstractList.height=_itemHeight*_dataProvider.length;
			
			if(_abstractList.height>_showCount*_itemHeight)
			{
				if(_verticalBar==null)
				{
					_verticalBar=new NxScrollBar(0,0,_showCount*_itemHeight,MinSliderSize,new NxScrollSkin(_skinStrArr[1],_skinStrArr[2]))
					_verticalBar.x=_itemWidth-_verticalBar.width;
					
					_verticalBar.sliderMinLen=MinSliderSize;
					
					_verticalBar.scrollDistance=_verticalBar.slideableScope/(_dataProvider.length-_showCount);
					
					_verticalBar.addEventListener(NxScrollEvent.UPDATE_AREA,scrollDisplay);
					
					_scrollBarSpace.addChild(_verticalBar);
					
					if(_isLocked)
					{
						_verticalBar.setSlider(_customPerDis);
					}
				}
				else
				{
					updateNxScrollPanel();
					_verticalBar.update(_showCount*_itemHeight,MinSliderSize);
				}
			}
			else
			{
				if(_verticalBar&&_verticalBar.parent)
				{
					_scrollBarSpace.removeChild(_verticalBar);
				}
			}
			updateOffset();
		}
		
		/**
		 * 每次_yOffset可能改变时都需要更新updateContainer（显示区域的容器）的位置。
		 * */
		private function updateContainer():void
		{
			_interActiveSpace.y=_iconSpace.y=_listBg.y=_dataSpace.y=_yOffset;
		}
//		/**
//		 * 向列表里写入数据
//		 * */
//		private function setData():void
//		{
//			if(dataProvider == null)
//			{
//				return;
//			}
//			
//			//临时行数
//			var tRow		:int	=	dataProvider.length;
//			
//			if(tRow == 0)
//			{
//				return;
//			}
//			
//			//文本框
//			var tLabel		:TextField;
//			
//			//图标
//			var tIcon		:NxClip;
//			
//			//数据
//			var tStr		:String;
//			
//			for(var i:int = 0 ; i < tRow ; i ++)
//			{
//				//通过“行_列”取到文本
//				tLabel	=	_itemSpace.getChildByName("label_"+i) as TextField;
//				
//				try
//				{
//					tStr	=	dataProvider[i]["label"];
//				}
//				catch(e:*)
//				{
//					tStr	=	null;
//				}
//				
//				if(tLabel == null && tStr != null)
//				{
//					tLabel		=	new TextField();
//					tLabel.name	=	"label_"+i;
////					addTxtEvent(tLabel);
//					_itemSpace.addChild(tLabel);
//				}
//				else if(tLabel != null && tStr == null)
//				{
////					removeTxtEvent(tLabel);
//					_itemSpace.removeChild(tLabel);
//				}
//				
//				if(tLabel != null && tStr != null)
//				{
//					tLabel.textColor	=	_defaultTxtColor;
//					
//					tLabel.htmlText	=	tStr;
//					
//					initTextControl(_itemAlign,tLabel,
//						new Rectangle(0,_itemHeight*i,
//							defaultWidth,_itemHeight));
//				}
//				
//				tIcon	=	_itemSpace.getChildByName("icon_"+i) as NxClip;
//								
//				if(dataProvider[i].icon != null && dataProvider[i].icon != "")
//				{
//					if(tIcon	==	null)
//					{
//						tIcon				=	new NxClip();
//						tIcon.mouseChildren	=	false;
//						tIcon.mouseEnabled	=	false;
//						_itemSpace.addChild(tIcon);
//					}					
//					tIcon.source	=	dataProvider[i].icon;
//					tIcon.x			=	iconOffSizeX;
//					tIcon.y			=	_itemHeight*i+iconOffSizeY;
//					tIcon.name		=	"icon_"+i;
//				}
//				else if(tIcon	!=	null)
//				{
//					_itemSpace.removeChild(tIcon);
//				}
//			}
//		}
		
//		/**
//		 * 刷新单元格布局
//		 * */
//		private function updateItemSize():void
//		{
//			//临时行数
//			var tRow		:int	=	dataProvider.length;
//			
//			//文本框
//			var tLabel		:TextField;
//			
//			for(var i:int = 0 ; i < tRow ; i ++)
//			{
//				tLabel	=	_itemSpace.
//					getChildByName("label_"+i) as TextField;
//				
//				if(tLabel != null)
//				{
//					initTextControl(_itemAlign,tLabel,
//						new Rectangle(0,_itemHeight*i,
//							_itemWidth,_itemHeight));
//				}		
//			}
//		}
		
		/**
		 * 初始化文本框
		 * */
		private function initTextControl(
			alignStr:String,
			val:TextField,
			area:Rectangle):void
		{
			//临时行数
			var tRow		:int	=	dataProvider.length;
			
			//如果出现滚动条
			if(tRow>showCount && _skinStrArr.length == 3)
			{
				area.width		=	area.width	-	
					(ResManager.newResClass(
						_skinStrArr[1])["track"] as MovieClip).width;
			}
			
			
			val.multiline		=	true;
//			
			val.mouseEnabled	=	false;
			val.selectable		=	false;
			
//			val.border			=	true;
			
			val.borderColor		=	0xffffff;
			
			if(val.htmlText.toLocaleLowerCase().search("<img") > -1)
			{
				var tStr	:String	=	val.htmlText;
				var tHeight	:Number;
				val.text		=	"a";
				val.autoSize	=	TextFieldAutoSize.LEFT;
				tHeight			=	val.height;
				
				val.autoSize	=	TextFieldAutoSize.NONE;
				val.wordWrap	=	true;
				val.htmlText	=	tStr;
				
				val.width		=	area.width;
				val.height		=	tHeight;
				val.x			=	area.x;
				val.y			=	area.y+(area.height-val.height)*.5;
				
				var tInfo:TextFormat	=	new TextFormat();
				tInfo.align				=	alignStr;
				val.setTextFormat(tInfo);
			}
			else
			{
				val.wordWrap		=	false;
//				//				var tempX:Number	=	val.x;
//				//				var tempY:Number	=	val.y;
//				
				val.autoSize		=	alignStr;
//				
				if(val.width > area.width)
				{
					val.autoSize	=	TextFieldAutoSize.NONE;
					val.width		=	area.width;
				}
//				
				val.y				=	area.y+(area.height-val.height)*.5;
//				
				switch(alignStr)
				{
					case TextFormatAlign.LEFT:
						val.x	=	area.x;
						break;
					case TextFormatAlign.RIGHT:
						val.x	=	area.x+(area.width-val.width);
						break;
					case TextFormatAlign.CENTER:
						val.x	=	area.x+(area.width-val.width)*.5;
						break;
				}
			}
		}
		
		
		private function leaveStageHandler(e:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE,leaveStageHandler);
			addEventListener(Event.ADDED_TO_STAGE,onStageHandler);
			_isDrawnAtOnce=false;
			
			_currentRollIndex = -1;
			
			sleep();
		}
		private function onStageHandler(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onStageHandler);
			addEventListener(Event.REMOVED_FROM_STAGE,leaveStageHandler);
			
			_isDrawnAtOnce=true;
			
			if(_tableChanged)
			{
				updateDisplay();
				_tableChanged=false;
			}
			
			invoke();
		}
		
		private function onClick(e:MouseEvent):void
		{
			var tItemIndex:int			=	getItemPosFromMouse(e.localX,e.localY);
			
			if(tItemIndex >= 0)
			{
				_currentSelect			=	tItemIndex;
				
				_downStateDis.visible=true;
				_downStateDis.y=_itemHeight*(_currentSelect-_indexOffset);
				
				var event:NxListEvent=new NxListEvent(
					NxListEvent.LIST_CLICK,dataProvider[currentSelect]);
				
				event.id=currentSelect;
				
				dispatchEvent(event);
				
			}
		}
		
		private function onMouseMove(e:MouseEvent):void
		{
			var tItemIndex:int			=	getItemPosFromMouse(e.localX,e.localY);
			
			if(tItemIndex == -1 && _currentRollIndex >= 0)
			{
				_overStateDis.visible	=	false;
//				_listBg.removeChild(_overStateDis);
				_currentRollIndex=-1;
				
				var event:NxListEvent=new NxListEvent(
					NxListEvent.ITEM_ROLLOUT,dataProvider[_currentRollIndex]);
				
				event.id=_currentRollIndex;
				dispatchEvent(event);
			}
			else if(tItemIndex >= 0)
			{
				if(_currentRollIndex == -1 || tItemIndex != _currentRollIndex)
				{
					_currentRollIndex		=	tItemIndex;
					
					_overStateDis.visible	=	true;
//					_listBg.addChild(_overStateDis);
					_overStateDis.y			=	_itemHeight*(_currentRollIndex-_indexOffset);
					
					var event2:NxListEvent=new NxListEvent(
						NxListEvent.ITEM_ROLLOVER,dataProvider[_currentRollIndex]);
					event2.id=_currentRollIndex;
					
					dispatchEvent(event2);
				}				
			}
		}
		
		private function onMouseMoveOut(e:MouseEvent):void
		{
			if(_currentRollIndex >= 0)
			{
//				_listBg.removeChild(_overStateDis);
				_overStateDis.visible	=	false;
				_currentRollIndex=-1;
				
				var event:NxListEvent=new NxListEvent(
					NxListEvent.ITEM_ROLLOUT,dataProvider[_currentRollIndex]);
				event.id=_currentRollIndex;
				
				dispatchEvent(event);
			}
		}
		
		private function mouseWheelhandle(e:MouseEvent):void
		{
//			_scrollPanel.HotArea.dispatchEvent(e);
			if(_verticalBar&&_verticalBar.parent)
			{
				_verticalBar.onMouseWheel(e);
			}
		}
		
		private function invoke():void
		{
//			_scrollPanel.addEventListener(NxScrollEvent.UPDATE_AREA,scrollDisplay);
			
			_listBg.addEventListener(MouseEvent.CLICK	,onClick);
			_listBg.addEventListener(MouseEvent.MOUSE_MOVE	,onMouseMove);
			_listBg.addEventListener(MouseEvent.ROLL_OUT	,onMouseMoveOut);
			addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheelhandle);
			
			if(_verticalBar&&_verticalBar.parent)
			{
				_verticalBar.addEventListener(NxScrollEvent.UPDATE_AREA,scrollDisplay);
			}
		}
		private function sleep():void
		{
			_listBg.removeEventListener(MouseEvent.CLICK	,onClick);
			_listBg.removeEventListener(MouseEvent.MOUSE_MOVE	,onMouseMove);
			_listBg.removeEventListener(MouseEvent.ROLL_OUT	,onMouseMoveOut);
			removeEventListener(MouseEvent.MOUSE_WHEEL,mouseWheelhandle);
			
			if(_verticalBar)
			{
				_verticalBar.removeEventListener(NxScrollEvent.UPDATE_AREA,scrollDisplay);
			}
		}
		
		/**
		 * 根据坐标判断所在单元格 
		 * @param valX	:	单元格所在容器坐标系内的x坐标
		 * @param valY	:	单元格所在容器坐标系内的y坐标
		 * @return 
		 * 
		 */		
		private function getItemPosFromMouse(valX:Number,valY:Number):int
		{
			
			//转换为相对于_listBg的坐标
//			valY=valY-_listBg.y;
			
			//临时行数
			var tRow		:int	=	_dataProvider.length>_showCount?_showCount+1:_dataProvider.length;
			
			var temp		:int;
			
			for(var i:int = 0 ; i < tRow ; i ++)
			{
				temp	=	_itemHeight*i;
				
				if(valY > temp && valY < temp+_itemHeight)
				{
					return i+_indexOffset;
				}
			}
			return -1;
		}
		
		
		private function getTotalRowNum():int
		{
			if(_dataProvider.length<_showCount)
			{
				return _showCount;
			}
			return _dataProvider.length;
		}

		/**默认文本颜色*/
		public function get defaultTxtColor():uint
		{
			return _defaultTxtColor;
		}

		/**
		 * @private
		 */
		public function set defaultTxtColor(value:uint):void
		{
			_defaultTxtColor = value;
			_txtFormat.color=_defaultTxtColor;
			
			if(_isDrawnAtOnce)
			{
				updateData();
			}
			else
			{
				_tableChanged=true;
			}
		}
	}
}